package share.service;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import share.dao.entity.RevenueReportDO;
import share.dao.mapper.RevenueReportDOMapper;
import share.service.model.RevenueReportModel;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RevenueReportServiceImpl implements RevenueReportService {

    @Autowired
    private RevenueReportDOMapper revenueReportDOMapper;

    @Override
    public List<RevenueReportModel> addRevenueReport(List<RevenueReportModel> revenueReportModelList) {

        List<RevenueReportDO> revenueReportDOs = convertFromModel(revenueReportModelList);

        revenueReportDOs.forEach(revenueReportDO -> {
            revenueReportDOMapper.insertSelective(revenueReportDO);
        });

        return revenueReportModelList;
    }

    private List<RevenueReportDO> convertFromModel(List<RevenueReportModel> revenueReportModelList) {
        List<RevenueReportDO> revenueReportDOs;
        if (revenueReportModelList == null || revenueReportModelList.isEmpty()) {
            revenueReportDOs = new ArrayList<>();
            return revenueReportDOs;
        }

        revenueReportDOs = revenueReportModelList.stream().map(revenueReportModel -> {
            RevenueReportDO revenueReportDO = new RevenueReportDO();
            BeanUtils.copyProperties(revenueReportModel, revenueReportDO);
            return revenueReportDO;
        }).collect(Collectors.toList());
        return revenueReportDOs;
    }
}
